package ecstore.client.customWidgets;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;

import ecstore.client.DataStorageService;
import ecstore.client.DataStorageServiceAsync;
import ecstore.client.dataOperation.ItemInfo;


public class SearchItemPanel extends Composite{
		private VerticalPanel mainSearchPanel = new VerticalPanel();
		private VerticalPanel verticalPanel = new VerticalPanel();
		private ItemListPanel itemListPanel = new ItemListPanel();
		Button btnSearch = new Button("New button");
		Button btnSearchAgain = new Button("New button");
		Button btnClear = new Button("New button");
		Button btnEditItem = new Button("New button");
		
		TextBox nameBox = new TextBox();
		ListBox categoryBox1 = new ListBox();
		ListBox categoryBox2 = new ListBox();
		TextBox brandBox = new TextBox();
		TextBox sizeBox = new TextBox();
		TextBox lowPriceBox = new TextBox();
		TextBox highPriceBox = new TextBox();
		
		String stringFilter = "";
		
		/**
		 * Create a remote service proxy to talk to the server-side Greeting service.
		 */
		private final DataStorageServiceAsync dataStorageService = GWT.create(DataStorageService.class);
		
		// Search results are stored in this list
		List<ItemInfo> resultItems = new ArrayList<ItemInfo>();
		
	public SearchItemPanel() {
		mainSearchPanel.setHeight("391px");
		initWidget(mainSearchPanel);
		mainSearchPanel.add(verticalPanel);
		
		HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		horizontalPanel_1.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		mainSearchPanel.add(horizontalPanel_1);
		horizontalPanel_1.setWidth("444px");
		
		
		btnSearchAgain.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				verticalPanel.clear();
				setUpSearchPanel();
				itemListPanel.clear();
			}
		});
		
		/** Show the results from server.
		 * 
		 */
		btnSearch.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(stringFilter.equalsIgnoreCase("")){
//					return;
				}
				verticalPanel.clear();
				verticalPanel.add(itemListPanel);
				btnSearch.setVisible(false);
				btnClear.setVisible(false);
				btnSearchAgain.setVisible(true);
				btnEditItem.setVisible(true);
				
				// create string filter
//				String itemName = nameBox.getText();
				String itemCategory = categoryBox1.getItemText(categoryBox1.getSelectedIndex());
//				String itemBrand = brandBox.getText();
//				String itemSize = sizeBox.getText();
//				float lowerPrice = Float.valueOf(lowPriceBox.getText());
//				float higherPrice = Float.valueOf(highPriceBox.getText());
				
				//TODO enhance the search string
//				stringFilter = "" + itemName + itemCategory;
				stringFilter = "category == " + itemCategory;
				
				dataStorageService.getItems(stringFilter, new AsyncCallback<List<ItemInfo>>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						System.out.println("faild to get items");
					}

					public void onSuccess(List<ItemInfo> result) {
						resultItems = result;
						Iterator<ItemInfo> itr = result.iterator();
						ItemInfo itemInfo = new ItemInfo();
						while(itr.hasNext()){
							itemInfo = (ItemInfo) itr.next();
							itemListPanel.addItem(itemInfo);

						}
						
					}
					
				});
				
				//Clear string filter
				stringFilter = "";
				
			}
		});
		
		btnSearch.setText("Search");
		horizontalPanel_1.add(btnSearch);
		btnSearchAgain.setText("Search again");
		horizontalPanel_1.add(btnSearchAgain);
		
		
		btnClear.setText("Clear");
		horizontalPanel_1.add(btnClear);
		
		
		btnEditItem.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				ItemInfo selectedItem = itemListPanel.getSelectedItem();
				ItemEditor itemEditor = new ItemEditor(selectedItem);
				mainSearchPanel.clear();
				mainSearchPanel.add(itemEditor);
			}
		});
		btnEditItem.setText("Edit item");
		horizontalPanel_1.add(btnEditItem);
		
		setUpSearchPanel();
	}
	
	private void setUpSearchPanel(){
		
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setSize("522px", "355px");
		
		Label lblSearchForAn = new Label("Search for an item");
		verticalPanel.add(lblSearchForAn);
		
		FlexTable flexTable = new FlexTable();
		verticalPanel.add(flexTable);
		flexTable.setSize("506px", "317px");
		
		Label lblItemname = new Label("Item name:");
		flexTable.setWidget(0, 0, lblItemname);
		
		flexTable.setWidget(0, 1, nameBox);
		
		Label lblCategory = new Label("Category:");
		flexTable.setWidget(1, 0, lblCategory);
		
		HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		flexTable.setWidget(1, 1, horizontalPanel_2);
		
		
		horizontalPanel_2.add(categoryBox1);
		categoryBox1.setWidth("113px");
		categoryBox1.setVisibleItemCount(1);
		categoryBox1.addItem("Tobacco");
		categoryBox1.addItem("Cigarettes");
		categoryBox1.addItem("Others");
		
		
		horizontalPanel_2.add(categoryBox2);
		categoryBox2.setWidth("135px");
		categoryBox2.setVisibleItemCount(5);
		
		Label lblBrand = new Label("Brand:");
		flexTable.setWidget(2, 0, lblBrand);
		
		
		flexTable.setWidget(2, 1, brandBox);
		
		Label lblSize = new Label("Size:");
		flexTable.setWidget(3, 0, lblSize);
		
		
		flexTable.setWidget(3, 1, sizeBox);
		
		Label lblPriceRange = new Label("Price range:");
		flexTable.setWidget(4, 0, lblPriceRange);
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		flexTable.setWidget(4, 1, horizontalPanel);
		
		
		horizontalPanel.add(lowPriceBox);
		
		Label lblTo = new Label("to");
		horizontalPanel.add(lblTo);
		
		
		horizontalPanel.add(highPriceBox);
		highPriceBox.setWidth("166px");
		
		//hide search again button
		btnSearchAgain.setVisible(false);
		btnEditItem.setVisible(false);
		btnClear.setVisible(true);
		btnSearch.setVisible(true);
		btnSearch.setFocus(true);
	}

}
